<template>
  <div class="user">
    <div class="userbox">
      <img :src="list.avatar | imgUrl" alt="" />
      <p>{{ list.nickName ? list.nickName : "匿名" }}</p>
      <span>积分：{{ list.score }}</span>
      <!-- 表面上的签到 -->
      <button @click="qianDao">签到</button>
      <router-link
        :to="{
          name: 'UserInfo',
        }"
        ><button>修改个人信息</button></router-link
      >
      <router-link
        :to="{
          name: 'UserPwd',
        }"
        ><button>修改密码</button></router-link
      >
    </div>
    <button class="logout" @click="logout">退出登录</button>
  </div>
</template>

<script>
import { removeToken } from "../utils/tools";
import { get } from "../utils/request";
import { checkIn } from "../services/user.js";
export default {
  data() {
    return {
      list: {},
    };
  },
  methods: {
    qianDao() {
      checkIn().then((res) => {
        // 签到成功后重新获取当前用户个人信息
        get("/api/v1/user/info").then((res) => {
          if (res.code === 1) {
            this.list = { ...res.data };
          } else {
            alert("服务器异常！");
          }
        });
      });
    },
    logout() {
      removeToken();
      this.$router.replace({
        name: "Home",
      });
    },
  },
  created() {
    get("/api/v1/user/info").then((res) => {
      if (res.code === 1) {
        this.list = { ...res.data };
      } else {
        alert("服务器异常！");
      }
    });
  },
};
</script>

<style scoped>
.userbox {
  height: 40vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-around;
  margin-top: 3rem;
}
img {
  width: 40vw;
  border-radius: 50%;
  border: 1px solid black;
}
button {
  background: pink;
  width: 100%;
  height: 2rem;
}
.logout {
  background: skyblue;
  height: 2rem;
  margin-top: 2rem;
}
a {
  display: block;
  width: 100%;
}
</style>